package controller;

import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import utils.Codification;
import utils.UserDAORemote;
import utils.UserlogDAORemote;
import utils.UsertypeDAORemote;
import dtos.UserDTO;
import dtos.UserlogDTO;
import dtos.UsertypeDTO;

/**
 * Session Bean implementation class Login
 */
@Stateless
@LocalBean
public class Login implements LoginRemote {

	/**
	 * Default constructor.
	 */

	@EJB
	UserDAORemote userDao;

	@EJB
	UserlogDAORemote userlogDao;

	@EJB
	UsertypeDAORemote usertypeDao;

	@PersistenceContext
	EntityManager em;

	public Login() {

	}

	/**
	 * This method verify if the user is on database and if exists returns his
	 * type
	 * 
	 * @param userDTO
	 *            - a UserDTO containing username and password
	 * @return a String containing either the error message or the type of the
	 *         user
	 */

	@Override
	public String verifyUser(UserDTO userDTO, String ending) {
		String mesaj;
		List<UserDTO> listUserDTO = userDao.getAllUsersByUsername(userDTO
				.getUsername());
		if (listUserDTO.isEmpty()) {
			mesaj = "Insucces";
		} else {
			if (!(listUserDTO.get(0).getPassword()
					.equals(userDTO.getPassword()))) {
				mesaj = "Pasword Mismatch";
			} else {
				if (listUserDTO
						.get(0)
						.getPassword()
						.equals(new Codification().md5(userDTO.getUsername()
								+ ending))) {
					mesaj = "Pending";
					System.out.println(" username este "
							+ userDTO.getUsername() + " terminatia este "
							+ ending);

				} else {
					if (listUserDTO.get(0).getIsActive() == 0) {
						mesaj = "Account inactiv";
					} else {
						mesaj = getUserType(listUserDTO.get(0));
					}
				}
			}

		}
		System.out.println("login.verifyUser returns " + mesaj);
		return mesaj;
	}

	@Override
	public String getUserType(UserDTO userDto) {
		UsertypeDTO usertypeDto = usertypeDao.getAllUsertypesByUserId(userDto
				.getId());
		return usertypeDto.getName();
	}

	/**
	 * This method change user password
	 * 
	 * @param userDTO
	 *            - a UserDTO that is currently on session
	 * @param retypedPassword
	 *            - the new password
	 * @return userDTO updated with the new password
	 * 
	 */
	@Override
	public UserDTO changePassword(UserDTO userDTO, String retypedPassword) {
		userDTO = setUser(userDTO);
		System.out.println("parola aproape gata "+retypedPassword);
		userDao.updateUserPasswordWithCodification(userDTO, retypedPassword);
		return userDTO;
	}

	/**
	 * This method also set the startTime on database
	 * 
	 * @param userDTO
	 *            - a UserDTO that contains only Username and password
	 * @return a UserDTO instantiated with all needed values
	 */
	@Override
	public UserDTO setUser(UserDTO userDTO) {
		UserDTO userDTOFinal = null;
		List<UserDTO> listUserDTO = userDao.getAllUsersByUsername(userDTO
				.getUsername());
		userDTOFinal = listUserDTO.get(0);
		System.out.println("got user: " + userDTOFinal.getName());
		UserlogDTO userlogDTO = new UserlogDTO();
		userlogDTO.setIdUser(userDTOFinal.getId());
		Date d = new Date();
		DateFormat df1 = new SimpleDateFormat("dd.MM.yyyy");
		DateFormat df2 = new SimpleDateFormat("HH:mm:ss");
		Date data;
		try {
			data = df1.parse(df1.format(d));
			userlogDTO.setDate(data);
			Time t = Time.valueOf(df2.format(d));
			userlogDTO.setStarttime(t);
			userlogDTO.setEndtime(t);

			userlogDao.insertUserlog(userlogDTO);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("got user: " + userDTOFinal.getId() + " "
				+ userDTOFinal.getName());

		return userDTOFinal;
	}

	/**
	 * This method set the end time on database
	 * 
	 * @param userDTO
	 *            - the UserDTO that is on session
	 * 
	 */
	@Override
	public void setEndTime(UserDTO userDTO) {
		Date d = new Date();
		DateFormat df2 = new SimpleDateFormat("HH:mm:ss");
		Time t = Time.valueOf(df2.format(d));
		List<UserlogDTO> lista = (List<UserlogDTO>) userlogDao
				.getAllUserlogsByUserId(userDTO.getId());
		for (UserlogDTO u : lista) {
			if (u.getStarttime().equals(u.getEndtime())) {
				u.setEndtime(t);
				userlogDao.updateUserlog(u);

			}

		}

	}

}